﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLFacade;
using BLFacade.entities;

namespace ProjectUI
{
    public partial class LoginForm : Form
    {
        IUserFacade userFacade;
        MainForm mdiForm;

        public LoginForm()
        {
            InitializeComponent();
            userFacade = FacadeHolder.UserFacade;
            //mdiForm = (MainForm)this.MdiParent;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            lblFailure.Text = "";
            try
            {
                mdiForm = (MainForm)this.MdiParent;
                //mdiForm.CurrentPerson = userFacade.GetUser(txtLogin.Text, txtPassword.Text);
                FacadeHolder.UserFacade.CurrentPerson = userFacade.GetUser(txtLogin.Text, txtPassword.Text);
                mdiForm.UserUpdate();
                this.Close();
            }
            catch (Exception ex)
            {             
               lblFailure.Text = ex.Message;
               txtPassword.Text = "";
            }

        }
        private void LoginForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            mdiForm = (MainForm)this.MdiParent;
            if (FacadeHolder.UserFacade.CurrentPerson == null) this.MdiParent.Dispose();
        }
    }
}
